<?php

namespace app\crontab\controller;

use app\common\model\AfterSales;
use app\common\model\Parameter;
use app\common\service\CommonOrderService;
use app\crontab\model\Order;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;

class CrontabOrder
{
    public function orderEndRewardExecute(){
        //防重复
        $file_name = Config::get("app.concurrency_fiel_path")."uploads/crontabOrder/orderEndRewardExecute.txt";
        $fz = file_exists($file_name);
        $buffer = 0;
        $str = time();
        $time = $str;
        if($fz){
            $fp=@fopen($file_name,'r');
            while(!feof($fp))
            {
                $buffer=fgets($fp,4096);
            }
            fclose($fp);
        }
        $buffer=$buffer + 40;
        if($buffer  > $time){
            Log::info('.订单完成到时间-进行分佣');
            exit;
        }
        $file_pointer = fopen($file_name,"w+");
        fwrite($file_pointer,$str);
        fclose($file_pointer);

        $valueTime = Parameter::parameterValue(["id"=>1]);
        $where[] = ["o.enable","=",1];
        $where[] = ["o.state",">=",3];
        $where[] = ["o.com_state","=",1];
        $where[] = ["o.commission_info","<>","[]"];
        $where[] = ["o.endtime","<",time() - $valueTime];
        $list = Order::orderList($where);
        if(!empty($list)){
            $ids = [];
            Db::startTrans();
            try {
                foreach ($list as $item){
                    $ids[] = $item["id"];
                    $orderComplainState = true;
                    $sale_after = AfterSales::dataInfo(["oid"=>$item["id"]]);
                    if(!empty($sale_after)){
                        $orderComplainState = false;
                    }
                    if($orderComplainState == true){
                        CommonOrderService::commissionSharing($item);
                    }
                }
                Db::commit();
                echo "SUCCESS";
            }catch (\Exception $e){
                echo "successs";
                Db::rollback();
            }
            Log::info('.订单完成到时间-进行分佣'.json_encode($ids,JSON_UNESCAPED_UNICODE));
       }else{
           echo "success";
       }
    }

    //订单超时自动收货
    public function orderTimeOutReceiving(){
        //防重复
        $file_name = Config::get("app.concurrency_fiel_path")."uploads/crontabOrder/orderTimeOutReceiving.txt";
        $fz = file_exists($file_name);
        $buffer = 0;
        $str = time();
        $time = $str;
        if($fz){
            $fp=@fopen($file_name,'r');
            while(!feof($fp))
            {
                $buffer=fgets($fp,4096);
            }
            fclose($fp);
        }
        $buffer=$buffer + 40;
        if($buffer  > $time){
            Log::record('订单签收超时-自动收货');
            exit;
        }
        $file_pointer = fopen($file_name,"w+");
        fwrite($file_pointer,$str);
        fclose($file_pointer);
        $valueTime = Parameter::parameterValue(["id"=>3]);
        $where[] = ["o.enable","=",1];
        $where[] = ["o.state","=",2];
        $where[] = ["o.com_state","=",1];
        $where[] = ["o.sendtime","<",time() - $valueTime];
        $list = Order::orderList($where);
        $ids = [];
        if(!empty($list)){
            foreach ($list as $item){
                $orderComplainState = true;
                $sale_after = AfterSales::dataInfo(["oid"=>$item["id"]]);
                if(!empty($sale_after)){
                    $orderComplainState = false;
                }
                if($orderComplainState == true){
                    $ids[] = $item["id"];
                }
            }
            if(empty($ids)){
                echo 'success';exit;
            }else{
                Db::startTrans();
                try {
                    \app\common\model\Order::editInfo([['id','in',implode(',',$ids)]],["state"=>3,"endtime"=>time()]);
                    Db::commit();
                    echo "SUCCESS";
                }catch (\Exception $e){
                    echo "successs";
                    Db::rollback();
                }
                Log::info('订单签收超时-自动收货:'.json_encode($ids,JSON_UNESCAPED_UNICODE));
            }

        }else{
            echo "successs";
        }
    }

}